﻿Imports System.Collections.Specialized
Imports System.Windows.Forms

Public Class AccountToAccount

    Private Sub btn支出关联1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn支出关联1.Click, btn支出关联2.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "账户一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,TITLE as 名称,Count as 金额,MEMO AS 备注 from CrashStatus order by 名称"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("金额")
            显示列.Add("备注")
            frm.Width = 300
            frm.Height = 300
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                If CType(sender, Button) Is Me.btn支出关联1 Then
                    txt支出关联1.Tag = CStr(frm.返回值(0))
                    txt支出关联1.Text = CStr(frm.返回值(1))
                    NM支出关联金额1.Value = CDec(frm.返回值(2))
                ElseIf CType(sender, Button) Is Me.btn支出关联2 Then
                    txt支出关联2.Tag = CStr(frm.返回值(0))
                    txt支出关联2.Text = CStr(frm.返回值(1))
                    NM支出关联金额2.Value = CDec(frm.返回值(2))
                End If
            End If
        End Using
    End Sub

    Private Sub btnUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsername.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "收入人一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,UserName as 名称,memo as 备注 from WysInputUser where type=0 order by 名称"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("备注")
            frm.Width = 250
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtUsername.Tag = CStr(frm.返回值(0))
                txtUsername.Text = CStr(frm.返回值(1))
            End If
        End Using
    End Sub

#Region "插入模式"

    Public Overrides Function DoAddCheck() As Boolean
        DoAddCheck = False

        If CStr(txt支出关联1.Tag).Trim = "" Then
            ShowMessageBox("转出账户不能为空")
            Me.btn支出关联1.Focus()
            Exit Function
        End If

        If CStr(txt支出关联2.Tag).Trim = "" Then
            ShowMessageBox("转入账户不能为空")
            Me.btn支出关联2.Focus()
            Exit Function
        End If

        If CStr(txt支出关联2.Tag).Trim = CStr(txt支出关联1.Tag).Trim Then
            ShowMessageBox("转入账户不能和转出账户一样")
            Me.btn支出关联2.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("转账金额不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        If numUpDown.Value > NM支出关联金额1.Value Then
            'ShowMessageBox("转账金额不能大于转出账户余额")
            'Me.numUpDown.Focus()
            'Exit Function
            If ShowMessageResult("转账金额大于转出账户余额,是否继续？") = Windows.Forms.DialogResult.No Then
                Me.numUpDown.Focus()
                Exit Function
            End If
        End If

        If txtMemo.Text.Trim = "" Then
            ShowMessageBox("转账说明不能为空")
            Me.txtMemo.Focus()
            Exit Function
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("收入用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If


        DoAddCheck = True
    End Function

    Public Overrides Function DoAddExist() As Boolean
        Return True
    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False

        If InsertCrashInOut(Me.txt支出关联1.Tag.ToString, numUpDown.Value, Me.txt支出关联2.Tag.ToString, Me.txtUsername.Tag.ToString) Then
            '更新关联数据          
            Me.bAdd = True '表示成功插入数据能刷新界面        
            ShowMessageBox("插入成功")
            ClearForm()
            DoInsert = True
        Else
            DoInsert = False
        End If
    End Function

#End Region

#Region "更新模式"

    Private Sub UpdateCrashInOut()
        Dim sb As New StringBuilder
        Dim dt As New DataTable
        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * from CrashInOut  where id='" + CStr(Me.参数列表("ID")) + "'")
        dt = ct.FillTable(rs)
        If dt.Rows.Count > 0 Then
            Dim cmdSub As ADODB.Command = New ADODB.Command
            cmdSub.CommandType = ADODB.CommandTypeEnum.adCmdText
            For Each dr As DataRow In dt.Rows
                If CStr(dr("InOutID")) <> "" Then
                    sb.Length = 0
                    sb.AppendLine("Update CrashStatus set Count=Count +" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("InOutID")) + "'")
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("InOutID")))

                    '插入金额 2022-11-20
                    Account_Detail.Update转出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                    Crash_Detail.Update转出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), CStr(dr("InOutID")), CDec(dr("CrashStatusCount")))
                End If

                If CStr(dr("CrashStatusID")) <> "" Then
                    sb.Length = 0
                    sb.AppendLine("Update CrashStatus set Count=Count -" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("CrashStatusID")) + "'")
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("CrashStatusID")))

                    '插入金额 2022-11-20
                    Account_Detail.Update转入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                    Crash_Detail.Update转入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), CStr(dr("CrashStatusID")), CDec(dr("CrashStatusCount")))
                End If
            Next
        End If

        ct.GetTable("DELETE from CrashInOut  where id='" + CStr(Me.参数列表("ID")) + "' and type=2 ")
    End Sub

    Public Overrides Function DoUpdate() As Boolean
        MyBase.DoUpdate()
        DoUpdate = False

        Dim sb As New StringBuilder

        UpdateCrashInOut()

        If InsertCrashInOut(Me.txt支出关联1.Tag.ToString, numUpDown.Value, Me.txt支出关联2.Tag.ToString, Me.txtUsername.Tag.ToString) Then
            '更新关联数据          
            Me.bAdd = True '表示成功插入数据能刷新界面        
            ShowMessageBox("更新成功")
            ClearForm()
            DoUpdate = True
            Me.Close()
        Else
            DoUpdate = False
        End If
    End Function

    Public Overrides Function DoUpdateCheck() As Boolean
        MyBase.DoUpdateCheck()
        DoUpdateCheck = False
        If CStr(txt支出关联1.Tag).Trim = "" Then
            ShowMessageBox("转出账户不能为空")
            Me.btn支出关联1.Focus()
            Exit Function
        End If

        If CStr(txt支出关联2.Tag).Trim = "" Then
            ShowMessageBox("转入账户不能为空")
            Me.btn支出关联2.Focus()
            Exit Function
        End If

        If CStr(txt支出关联2.Tag).Trim = CStr(txt支出关联1.Tag).Trim Then
            ShowMessageBox("转入账户不能和转出账户一样")
            Me.btn支出关联2.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("转账金额不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        If numUpDown.Value > NM支出关联金额1.Value Then
            If ShowMessageResult("转账金额大于转出账户余额,是否继续？") = Windows.Forms.DialogResult.No Then
                Me.numUpDown.Focus()
                Exit Function
            End If
        End If

        If txtMemo.Text.Trim = "" Then
            ShowMessageBox("转账说明不能为空")
            Me.txtMemo.Focus()
            Exit Function
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("收入用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If
        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        DoUpdateExist = False
        MyBase.DoUpdateExist()
        If CStr(Me.参数列表("ID")).Trim = "" Then
            ShowMessageBox("没有选择任何记录")
            Exit Function
        End If
        DoUpdateExist = True
    End Function

    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()
        Me.Text = "收入更新"
        ClearForm()
        Dim rs As ADODB.Recordset = Nothing
        Dim sb As New StringBuilder

        sb.AppendLine(" SELECT CrashInOut.*  ")
        sb.AppendLine(" ,CrashStatus1.Title as Name1  ")
        sb.AppendLine(" ,CrashStatus1.ID as ID1  ")
        sb.AppendLine(" ,CrashStatus2.ID as ID2  ")
        sb.AppendLine(" ,CrashStatus2.Title as Name2  ")
        sb.AppendLine(" ,WysInputUser.username username1")
        sb.AppendLine(" FROM CrashInOut ")
        sb.AppendLine(" left join CrashStatus CrashStatus1 on CrashStatus1.ID=CrashInOut.InOutID ")
        sb.AppendLine(" left join CrashStatus CrashStatus2 on CrashStatus2.ID=CrashInOut.CrashStatusID ")
        sb.AppendLine(" LEFT JOIN WysInputUser ")
        sb.AppendLine(" ON CrashInOut.username=WysInputUser.id ")
        sb.AppendLine(" WHERE CrashInOut.id='" + CStr(Me.参数列表("ID")) + "'")

        rs = ct.GetTable(sb.ToString)

        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Me.dt1.Text = CMainModule.CmnToStr(rs.Fields("DateTime").Value)
                Me.txtMemo.Text = CMainModule.CmnToStr(rs.Fields("Memo").Value)
                Me.numUpDown.Text = CDec(rs.Fields("CrashStatusCount").Value)
                Me.txtUsername.Text = UTIL.CUtil.ConvertText(rs.Fields("UserName1").Value)
                Me.txtUsername.Tag = UTIL.CUtil.ConvertText(rs.Fields("UserName").Value)


                Me.txt支出关联1.Text = UTIL.CUtil.ConvertText(rs.Fields("Name1").Value)
                Me.txt支出关联1.Tag = UTIL.CUtil.ConvertText(rs.Fields("ID1").Value)

                sb.Length = 0
                sb.AppendLine("select * from CrashStatus where id='" + Me.txt支出关联1.Tag.ToString() + "'")
                Dim rs1 As ADODB.Recordset = ct.GetTable(sb.ToString())
                If rs1.EOF = False Then
                    Me.NM支出关联金额1.Value = CDec(rs1.Fields("Count").Value)
                End If

                Me.txt支出关联2.Text = UTIL.CUtil.ConvertText(rs.Fields("Name2").Value)
                Me.txt支出关联2.Tag = UTIL.CUtil.ConvertText(rs.Fields("ID2").Value)
                sb.Length = 0
                sb.AppendLine("select * from CrashStatus where id='" + Me.txt支出关联2.Tag.ToString() + "'")
                Dim rs2 As ADODB.Recordset = ct.GetTable(sb.ToString())
                If rs1.EOF = False Then
                    Me.NM支出关联金额2.Value = CDec(rs2.Fields("Count").Value)
                End If

                Exit Sub
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        End If
        rs = Nothing
    End Sub

#End Region

#Region "清除窗体"
    Public Sub ClearForm()

        Me.numUpDown.Value = 0
        'Me.dt1.Value = Now
        Me.txtMemo.Text = ""

        Me.txtUsername.Text = ""
        Me.txtUsername.Tag = ""

        Me.txt支出关联1.Text = ""
        Me.txt支出关联1.Tag = ""

        Me.txt支出关联2.Text = ""
        Me.txt支出关联2.Tag = ""

        NM支出关联金额1.Value = 0
        NM支出关联金额2.Value = 0

    End Sub
#End Region

#Region "加载事件"

    Public Overrides Sub InitAdd()
        ClearForm()
    End Sub

#End Region

    Private Function InsertCrashInOut(ByVal 转出ID As String, ByVal 支出金额 As Decimal, ByVal 转入ID As String, ByVal 用户ID As String) As Boolean

        Dim sb As New StringBuilder
        Dim cmd As ADODB.Command = New ADODB.Command
        sb.Length = 0
        sb.Length = 0
        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, System.Guid.NewGuid.ToString("D"))
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, 转出ID)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, 转入ID)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = 支出金额
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, 用户ID)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, dt1.Value.ToString())
        cmd.Parameters.Append(pa)

        sb.AppendLine("Insert into CrashInOut values(?,?,?,?,?,2,?,?,?,' ')")

        cmd.CommandText = sb.ToString
        If ct.ExcCommand(cmd) Then
            cmd = New ADODB.Command
            sb.Length = 0
            sb.AppendLine("Update CrashStatus set Count=Count +" & 支出金额.ToString & " Where id='" & 转入ID & "'")
            cmd.CommandText = sb.ToString
            UpdateCrashOptime(ct, 转入ID)

            '插入金额 2022-11-20
            Account_Detail.Update转出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)
            Account_Detail.Update转入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txtUsername.Tag.ToString.Trim, numUpDown.Value)

            Crash_Detail.Update转出_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联1.Tag.ToString, Me.numUpDown.Value)
            Crash_Detail.Update转入_加(ct, dt1.Value.ToString("yyyy-MM-dd"), Me.txt支出关联2.Tag.ToString, Me.numUpDown.Value)

            If (ct.ExcCommand(cmd)) Then
                sb.Length = 0
                sb.AppendLine("Update CrashStatus set Count=Count -" & 支出金额.ToString & " Where id='" & 转出ID & "'")
                cmd.CommandText = sb.ToString
                UpdateCrashOptime(ct, 转出ID)
                Return ct.ExcCommand(cmd)
            End If
        End If
        Return False

    End Function
End Class
